#### Comparing results of popularity measures
## Separate regression for each race: ------
general_reg_separe_race <- function(dep_var, grade_var) {
  # dep_var = "ssi_same_negro_na_2_std"
  # grade_var = "grades_score"
  
  basic_reg <- paste0(dep_var, "~", paste0(grade_var))
  
  Form_basic <- formula(basic_reg)
  
  # Individual control
  Form_ind <- formula(paste(basic_reg, ind_controls, sep = "+"))
  
  # SES control
  Form_ses <- formula(paste(basic_reg, ses_controls, sep = "+"))
  
  # Individual + SES control
  Form_ind_ses <- formula(paste(basic_reg, ind_controls, 
                                ses_controls, sep = "+"))
  
  # Individual controls + Classroom FE
  Form_ind_class_fe <- formula(paste(basic_reg, ind_controls, 
                                     class_fe, sep = "+"))
  
  # SES controls + Classroom FE
  Form_ses_class_fe <- formula(paste(basic_reg, ses_controls, 
                                     class_fe, sep = "+"))
  
  # Ind. + SES controls + Classroom FE
  Form_ind_ses_class_fe <- formula(paste(basic_reg, ind_controls,
                                         ses_controls, class_fe, 
                                         sep = "+"))
  
  list_specifications <- c(Form_basic, Form_ind, Form_ses, Form_ind_ses,
                           Form_ind_class_fe, Form_ses_class_fe, 
                           Form_ind_ses_class_fe) 
  
  results_final <- list()
  for (i in c(1)) {
    # i = 1
    print(i)
    results <- lapply(list_specifications, function(x) (lm(x, data = data, subset = (race == i))))
    results_r <- lapply(results, robust_se, "HC3")
    #results_c <- lapply(results, cluster_se, "class_id")
    results_c <- NA
    
    results_final[[i]] <- list(results, results_r, results_c)  
  }
  print(4)
  results <- lapply(list_specifications, function(x) (lm(x, data = data, subset = (negro_na == 1))))
  results_r <- lapply(results, robust_se, "HC3")
  #results_c <- lapply(results, cluster_se, "class_id")
  results_c <- NA
  
  
  results_final[[4]] <- list(results, results_r, results_c)  
  
  return(results_final)
}


sep_same_linear <- general_reg_separe_race("ssi_same_negro_na_2_std", "grades_score")
sep_other_linear <- general_reg_separe_race("ssi_other_race_negro_na_2_std", "grades_score")
sep_all_linear <- general_reg_separe_race("ssi_all_race_2_std", "grades_score")

list_regs_c <- list(sep_same_linear[[1]][[2]][[7]], sep_same_linear[[4]][[2]][[7]], 
                  sep_other_linear[[1]][[2]][[7]],   sep_other_linear[[4]][[2]][[7]],   
                  sep_all_linear[[1]][[2]][[7]], sep_all_linear[[4]][[2]][[7]])

stargazer(list_regs_c,
          column.separate = c(2,2,2),
          dep.var.labels.include = F,
          dep.var.caption = "Social status",
          column.labels = c("Same race", "Other race", "All races"),
          omit = variables_to_omit, 
          covariate.labels  = c("Grades"),
          omit.stat = c("all", "rsq","ser"),
          add.lines = list(c("Racial group", "White", "Nonwhite", "White", "Nonwhite",
                             "White", "Nonwhite")),
          font.size = "footnotesize", type = "text")

stargazer(list_regs_c,
          column.separate = c(2,2,2),
          dep.var.labels.include = F,
          dep.var.caption = "Social status",
          covariate.labels  = c("Grades"),
          column.labels = c("Same race", "Other race", "All races"),
          omit = variables_to_omit, 
          omit.stat = c("all", "rsq","ser"),
          add.lines = list(c("Racial group", "White", "Nonwhite", "White", "Nonwhite",
                             "White", "Nonwhite")),
          font.size = "footnotesize", type = "latex", 
          out = "tables/robustness_sep_linear.tex",
          label = "robustness_sep_linear",
          title = "Robustness of results to separate regressions for racial groups - Linear specification")
